package cn.iocoder.yudao.framework.mybatis.core.util;

import org.jasypt.encryption.pbe.PooledPBEStringEncryptor;
import org.jasypt.encryption.pbe.config.SimpleStringPBEConfig;

public class JasyptUtils {

  public static String encrypt(String password, String message) {
    final String result = getPooledPBEStringEncryptor(password).encrypt(message);
    return result;
  }

  public static String decrypt(String password, String message) {
    final String result = getPooledPBEStringEncryptor(password).decrypt(message);
    return result;
  }

  public static PooledPBEStringEncryptor getPooledPBEStringEncryptor(String password) {
    final SimpleStringPBEConfig config = getSimpleStringPBEConfig(password);

    final PooledPBEStringEncryptor encryptor = new PooledPBEStringEncryptor();
    encryptor.setConfig(config);
    return encryptor;
  }

  private static SimpleStringPBEConfig getSimpleStringPBEConfig(String password) {
    final SimpleStringPBEConfig config = new SimpleStringPBEConfig();
    config.setPassword(password);
    config.setAlgorithm("PBEWithMD5AndDES");
    config.setKeyObtentionIterations("1000");
    config.setPoolSize("1");
    config.setProviderName("SunJCE");
    config.setSaltGeneratorClassName("org.jasypt.salt.RandomSaltGenerator");
    config.setStringOutputType("base64");
    return config;
  }

}